package com.huiminpay.transaction.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 有点儿意思
 * @Description:
 */
@Controller
public class PayTestController {

    /**
     * 支付宝网关地址 测试地址：https://openapi.alipaydev.com/gateway.do  正式地址没有dev
     */
    private String serverUrl = "https://openapi.alipaydev.com/gateway.do";
//    private String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    /**
     * 支付宝应用id
     */
    String appId = "2021000122696041";

    /**
     * 应用私钥
     */
    String privateKey ="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCaGzokR2AV6oDm9Zglj3J8oE0CpwisBNTeY8TjWjPAZmBHwz5sn58rn6e4tcx/atm4N9mQnyc7nFZJq9oog6mHfkNdlB07DvjHUN0zFgmqyU139VAFr9Lt3fB/2J9M1AW224groAj8jzCcgFF9Uaf6t8cpoDodmEekVqFl0/NIaUbgouXo7cd0HCgBxgbjHqFmwwszYjIWKwjZ0t9j6kX8YtOI4Q7ByOry7qVR+7/E4AAmmCTxVT4od0zpMNOGaFx9pONVFrcJ7E4kbM7K0Hl7+35K52EW9UnutNg+qkwL/Z2WZIeoD4S6X+JNZWLxCoyua+x6gud6/d+Isf8K/oWBAgMBAAECggEBAJa1Ydrw4osMVxuHu0+gRbqTmjyp1dVEtFj5ASfGoy3Z8Pa9fmMEh8lFzeTaBi/l8x4xoFfLDbdCYTCTHDra6ARV+tOdcii7r5hdALZhKvsW+ugBxEyLcqCll/mvg7rC7wOxFi5zx9YWwteKtib0oG6VI/UVeUBQGGAjSJwo95crt72bqgaShDUMhXFrlCS2pKQOyOVQw7YOTtAKGeE9pvN3gx54N2RDtZ2D1p8InbwIosk64iD89a+Ov9BI9ReU9nl7Wrbb/znt1g7I2hKIf6x3r1pQDV2KxM0e34O7JfJIyWtIIJ6Gy3c3C/w0ML7m8tdSH5hbFIR5h2moZ2LkRAECgYEA2cLnahkIAa3LC1Pkwa429v/5qU+HD1LMCBgBBuDfwX6BcAJgGsL0iBpby86KlByJvQixR+Ewdn+vTHUnLcEZ89YgiLltRQXOZpj02ibdE98c3xn9bfEbn33YXFHqlpvSNbP6MRF+pgdKenqJkx8rUWl8CPIEUWpjzpNlAjflPJECgYEAtSrR5qAn8DYfB7GRCxztorjEYLUVp9MZtQQ/odtpc8qPcr5csZvjMHcr/S2+i1NQTrVr2+GbMOXgLXLN634KXtGX3HHOpcYjPaiVRSKbPilfy/+ot4Pj+kM3V8pS3dKHe41ImFrgx3HnPDGBgXzwdroLfEeZ3xj/YIbun4Fs8fECgYBHErQRDuZsFdslcVZCWKgb6eQEfK5qM+3f3mXHZvHg53c7parUUTkaiu/XDyxvtvjFt+aUvUziAD6ox7vn1RSxXvvzZAHj+0coeVfBM9oIOFPmBUeP8JfBcIODbkEQsv7jBEDubvVVHyAR5ZitagjrY3BWQxdhs0WsELNxC+lH4QKBgQCd1VhZHF6vH/eYtFIw5FsD0QUFW74qH/fdhH4LfSQ6BNdEmCQZeD5OEwT35cqDh2oHqHb3cW3LA/pynfpYl85hHSi4nU3zjtNU/HJPcP7fARy8lVdfyj/r4HNuwI3BQ5FVSplcYpBCMjHKdzbkoPy98wfelmKvww96ARQTUqafgQKBgQCNJdhurxnV24y8No4FcOXJ8kXnht7Qzxy/JpTIn6ZK2XCgUO6LIWSGGwG/RXkAf8mtqm4Vld44nVOzlLkXauwa6WtQuYIABpjvufgx26QeRoLnNkHr5uKwWha1ibTLoEVyu5Nhqoa6iF4aRnYNh7jPYGslGyNqWvjKp6o6ShAqbA==";

    /**
     * 格式化方式
     */
    String format = "json";

    /**
     *  字符集
     */
    String charset = "utf-8";

    /**
     * 支付宝公钥
     */
    String alipayPublicKey ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1ZWub6R/98a7UFiMrVN4byzOYllORNMl/1xMK+bxdM+wFcP8KGqrzLjiQQ2ElXTnoJ7rLFxlU2BE4/YclkEj6MFosNiJmhlD9C2gG48qCGCD+27bqHUZ5evhbT2CNTUUthV075ilPxd21gwNQHnbzxaZxqy2awUF7A3l4/7XI1DKferdAZYZtlHTa8QM7Pk2TxN/MdF+Ev9vjNYUoC4aVSZQzHFhyaCW++bMCVIlmn/HQz1rGpajrw8QRHwcoQNfENCIumDymVIqlHJjOUBNGIoru6Gs6j02xJVIxJfnvO9nErx/6CbsiUKuQg4PGrd8Cwzi1aia9hQJgT3+I5saVwIDAQAB";
    /**
     * 签名类型
     */
    String signType = "RSA2";

    @GetMapping("/alipayTest")
    public void alipayTest(HttpServletResponse response){

        // AlipayClient对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey,
                format, charset, alipayPublicKey, signType);

        //手机网站支付的请求对象
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        //设置公共参数
        //异步接收地址 仅支持http/https，公网可访问
        request.setNotifyUrl("");
        //同步跳转地址
        request.setReturnUrl("");
        //设置业务参数
        JSONObject bizContent = new JSONObject();
        //商户订单号
        bizContent.put("out_trade_no", "70501111111S001111118");
        // 支付金额
        bizContent.put("total_amount", 9999.9);
        // 订单标题
        bizContent.put("subject", "鸟莎拉蒂 syms");
        // 手机网站支付固定产品码：QUICK_WAP_WAY
        bizContent.put("product_code", "QUICK_WAP_WAY");
        request.setBizContent(bizContent.toString());

        try {
            AlipayTradeWapPayResponse payResponse = alipayClient.pageExecute(request);
            String form = payResponse.getBody();
            // 回写到客户端 调用支付宝
            response.setContentType("text/html;charset=" + charset);
            //直接将完整的表单html输出到页面
            response.getWriter().write(form);
            response.getWriter().flush();
            response.getWriter().close();
        }  catch (AlipayApiException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}